package cn.com.egova.demoapplication;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.egova.demoapplication.base.BaseActivity;

import static cn.com.egova.demoapplication.R.animator.animator_alpha;
import static cn.com.egova.demoapplication.R.animator.animator_translate;

public class PropertyAnimaActivity extends BaseActivity {
    @InjectView(R.id.btn_alpha_xml)
    Button btnAlphaXml;
    @InjectView(R.id.btn_translate_xml)
    Button btnTranslateXml;
    @InjectView(R.id.btn_rotate_xml)
    Button btnRotateXml;
    @InjectView(R.id.btn_scale_xml)
    Button btnScaleXml;
    @InjectView(R.id.btn_combile_xml)
    Button btnCombileXml;
    @InjectView(R.id.btn_alpha_java)
    Button btnAlphaJava;
    @InjectView(R.id.btn_translate_java)
    Button btnTranslateJava;
    @InjectView(R.id.btn_rotate_java)
    Button btnRotateJava;
    @InjectView(R.id.btn_scale_java)
    Button btnScaleJava;
    @InjectView(R.id.btn_combile_java)
    Button btnCombileJava;
    @InjectView(R.id.iv)
    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property_anima);
        ButterKnife.inject(this);
    }

    @Override
    public boolean isBackIconVisible() {
        return true;
    }

    @OnClick({R.id.btn_alpha_xml, R.id.btn_translate_xml, R.id.btn_rotate_xml, R.id.btn_scale_xml, R.id.btn_combile_xml})
    public void animatorByXml(View view) {
        int resid = 0;
        switch (view.getId()) {
            case R.id.btn_alpha_xml:
                resid = animator_alpha;
                break;
            case R.id.btn_translate_xml:
                resid = animator_translate;
                break;
            case R.id.btn_rotate_xml:
                resid = R.animator.animator_rotate;
                break;
            case R.id.btn_scale_xml:
                resid = R.animator.animator_scale;
                break;
            case R.id.btn_combile_xml:
                resid = R.animator.animator_combile;
                break;
        }

        Animator animator = AnimatorInflater.loadAnimator(this, resid);
        animator.setTarget(iv);
        animator.start();
    }


    @OnClick({R.id.btn_alpha_java, R.id.btn_translate_java, R.id.btn_rotate_java, R.id.btn_scale_java, R.id.btn_combile_java})
    public void animatorByJava(View view) {
        //ObjectAnimator:采用new的方式比较简单,它的from to 参数是可变长度,可以任意长.
        ObjectAnimator animator = null;
        AnimatorSet set = null;
        switch (view.getId()) {
            case R.id.btn_alpha_java:
                animator = ObjectAnimator.ofFloat(iv, "alpha", 1.0f, 0f, 1.0f);

                break;
            case R.id.btn_translate_java:
                animator = ObjectAnimator.ofFloat(iv, "translationX", 0f, 100f, 0f);

                break;
            case R.id.btn_rotate_java:
                animator = ObjectAnimator.ofFloat(iv, "rotation", 0f, 360f, 0f);

                break;
            case R.id.btn_scale_java:
                animator = ObjectAnimator.ofFloat(iv, "scaleX", 0f, 1.5f, 0f);

                break;
            case R.id.btn_combile_java:
                ObjectAnimator animator_translatex = ObjectAnimator.ofFloat(iv, "translationX", 0f, 100f);
                ObjectAnimator animator_translatey = ObjectAnimator.ofFloat(iv, "translationY", 0f, 100f);
                ObjectAnimator animator_scalex = ObjectAnimator.ofFloat(iv, "scaleX", 0f, 1.5f);
                ObjectAnimator animator_scaley = ObjectAnimator.ofFloat(iv, "scaleY", 0f, 1.5f);
                ObjectAnimator animator_alpha = ObjectAnimator.ofFloat(iv, "alpha", 1.0f, 0f, 1.0f);
                set = new AnimatorSet();
                set = setListener(set);
//              安排执行顺序的方式1:
//                set.playTogether(animator_translatex, animator_translatey);   //同时执行
//                set.playSequentially(animator_translatex, animator_scalex, animator_alpha); //按顺序执行
//                set.playTogether(animator_scalex, animator_scaley);

//                安排执行顺序的方式2:
                set.play(animator_translatex).before(animator_scalex);
                set.play(animator_translatex).with(animator_translatey);
                set.play(animator_scalex).with(animator_scaley);
                set.play(animator_alpha).after(animator_scaley);

                set.setDuration(5000);
                set.start();


                break;
        }

        if (set == null) {
            animator.setDuration(3000);
            animator.setTarget(iv);
            animator.start();
        }
    }



    private AnimatorSet setListener(AnimatorSet set){
        //设置监听.
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                Toast.makeText(PropertyAnimaActivity.this,"动画开始了!", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                Toast.makeText(PropertyAnimaActivity.this,"动画结束了!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        return set;
    }
}
